/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.deliveryorder;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.utility.date.DateUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.common.pipeline.session.SessionInfo;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsDeliveryOrderBaseMapper;
import com.lightkits.wms.database.model.BdDeliveryOrderModel;
import com.lightkits.wms.dto.deliveryorder.QueryDeliveryOrderRequestType;
import com.lightkits.wms.dto.deliveryorder.QueryDeliveryOrderResponseType;
import com.lightkits.wms.dto.deliveryorder.model.DeliveryViewDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * QueryDeliveryOrderExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/2/27 12:59
 */
@Component
public class QueryDeliveryOrderExecutor implements
        ServiceExecutor<QueryDeliveryOrderRequestType, QueryDeliveryOrderResponseType> {

    private final Logger logger = LoggerFactory.getLogger(QueryDeliveryOrderExecutor.class);
    private final WmsDeliveryOrderBaseMapper bdDeliveryOrderBaseMapper;

    @Autowired
    public QueryDeliveryOrderExecutor(WmsDeliveryOrderBaseMapper bdDeliveryOrderBaseMapper) {
        this.bdDeliveryOrderBaseMapper = bdDeliveryOrderBaseMapper;
    }

    /**
     * @param request 请求参数
     * @return QueryDeliveryOrderResponseType
     */
    @Override
    public QueryDeliveryOrderResponseType service(QueryDeliveryOrderRequestType request) {
        QueryDeliveryOrderResponseType response = new QueryDeliveryOrderResponseType();
        response.setSuccessful(true);
        if (ObjectUtil.isNullOrEmpty(request.getQueryDate()) || ObjectUtil.isNullOrEmpty(request.getQueryParams())) {
            response.setSuccessful(false);
            response.setMessage("the params is empty");
            return response;
        }
        if (!DateUtil.isDate(request.getQueryDate(), DateUtil.DEFAULT_DATE_PATTERN)) {
            response.setSuccessful(false);
            response.setMessage("the queryDate is invalid");
            return response;
        }
        List<BdDeliveryOrderModel> models;

        if (!ObjectUtil.isNullOrEmpty(request.getDeliveryNumOrId())) {
            models = bdDeliveryOrderBaseMapper.queryDeliveryOrderByDate(
                    null,
                    null,
                    request.getDeliveryNumOrId(),
                    SessionInfo.languageCode());
        } else {

            if (request.getQueryParams().equals("MKDate")) {
                models = bdDeliveryOrderBaseMapper.queryDeliveryOrderByDate(
                        null,
                        request.getQueryDate(),
                        request.getDeliveryNumOrId(),
                        SessionInfo.languageCode());
            } else {
                models = bdDeliveryOrderBaseMapper.queryDeliveryOrderByDate(
                        request.getQueryDate(),
                        null,
                        request.getDeliveryNumOrId(),
                        SessionInfo.languageCode());
            }
        }

        List<DeliveryViewDTO> deliveryViewDTOS = new ArrayList<>();
        models.forEach(target -> {
            deliveryViewDTOS.add(convertViewDto(target));
        });
        response.setMountSize(String.valueOf(deliveryViewDTOS.size()));
        response.setDeliveryViewDTOS(deliveryViewDTOS);
        return response;
    }

    /**
     * 类型转换
     *
     * @param model model
     * @return DeliveryViewDTO
     */
    private DeliveryViewDTO convertViewDto(BdDeliveryOrderModel model) {
        DeliveryViewDTO dto = new DeliveryViewDTO();
        try {
            dto.setDeliveryId(model.getDeliveryId());
            dto.setDeliveryNum(model.getDelioveryNum());
            dto.setOrderStatus(model.getOrderStatus());
            dto.setItemId(model.getItemId());
            dto.setItemCode(model.getItemCode());
            dto.setItemDesc(model.getItemDesc());
            dto.setPrimaryUom(model.getPrimaryUom());
            dto.setPrimaryUomText(model.getPrimaryUomText());
            dto.setCustomerId(model.getCustomerId());
            dto.setCustomerName(model.getCustomerName());
            dto.setCustomerItem(model.getCustomerItem());
            dto.setExeQty(model.getExeQty());
            dto.setOrderStatusText(model.getOrderStatusText());
            dto.setMakeOrderQty(model.getMakeOrderQty());
            if (!ObjectUtil.isEmpty(model.getMakeOrderDate())) {
                dto.setMakeOrderDate(DateUtil.toDateString(
                        model.getMakeOrderDate(), DateUtil.DEFAULT_DATETIME_PATTERN));
            }
            dto.setCustomerItem(model.getCustomerItem());
            dto.setDemandQty(model.getDemandQty());
            dto.setDoId(model.getDoId());
            dto.setOrgId(model.getOrgId());
            dto.setDoNum(model.getDoNum());
            if (!ObjectUtil.isEmpty(model.getDemandDate())) {
                dto.setDemandDate(DateUtil.toDateString(
                        model.getDemandDate(), DateUtil.DEFAULT_DATE_PATTERN));
            }

        } catch (Exception e) {
            logger.error("convertViewDto.error", e);
            throw new LightKitsBizException(e);
        }
        return dto;
    }

}
